




var	selectedRow;
var selectedBlock;
var oLastOverlappedBlock;
var oGanttContainer = null;
var oSeparator = null;

var bScrollSync = false;
var bChangingGrid = false;
var bChangingGuides = false;
var oHeaderRowPadding = null ;

function GanttJsWindowOnLoad()
{
ConfigureGrid();
oGanttContainer = crmGanttTd;
oSeparator = separator;

DateRangeEnd.attachEvent("onchange", EndDateChanged);
DateRangeStart.attachEvent("onchange", StartDateChanged);
ResizeGuides(document.all.ganttContent);

UpdateConflictButtonState();
oHeaderRowPadding = window.document.all.ganttHeaderRowTop.lastChild ;
if ( LOCID_UI_DIR=="RTL" )
_iStartPosition = adjustBiDiStartPos();

switch (window.parent.SubAreaType())
{
case "appforsched":
case "schedules":
ScrollToOffset(_iStartPosition, true);
break;
}
LoadSubjectsAsynchronously();
}
window.attachEvent('onload', GanttJsWindowOnLoad);


var regexpForOverlapping = new RegExp("(\s*((4201|4214)\s*\,\s*)*\s*(4201|4214)\s*)");














function IsAppointmentOrServiceAppointment(sType)
{
var foundArray = regexpForOverlapping.exec(sType);
if ( foundArray == null )
{
return false;
}


if ( foundArray.index != 0 || foundArray.lastIndex != sType.length )
{
return false;
}
return true;
}











function LoadSubjectsAsynchronously()
{
var oidArray = new Array();
oidArray.type = "guid";
var idArray = new Array();
idArray.type = "string";
var count = 0;
for (var i = 0 ; i < gridBodyTable.document.all.length ; i++)
{
o = gridBodyTable.document.all[i];

if (o.tagName != "DIV" || o.otype == undefined || o.className == undefined)
{
continue;
}

if ( typeof(o.otype)!="string" || typeof(o.className)!="string")
{
continue;
}

if (o.className.indexOf("ganttBlockBase") == -1 && o.className.indexOf("ganttBlockOverlapped") == -1)
{
continue;
}

if (IsAppointmentOrServiceAppointment(o.otype) == false)
{
continue;
}

var oidArrayFromFoundUIObject = o.oid.split(",");
oidArrayFromFoundUIObject.type = "string";
for (var j = 0 ; j < oidArrayFromFoundUIObject.length ; j++)
{
oidArray[count] = oidArrayFromFoundUIObject[j];
idArray[count] = o.id;
count++;
}
}
if (count > 0)
{
var command = new RemoteCommand("GanttControlWebService", "GetCommitmentSubjects");
command.SetParameter("activityIds", oidArray);
command.SetParameter("controlIds", idArray);
command.Execute(LoadSubjectsAsynchronouslyCallback);
}
}











function SetTitleAndInnerTextIfNeeded(controlId, subject,overlappedAppointment)
{
var oGantt = document.getElementById(controlId);
var title = oGantt.title;














var maxI = 32;
for (var i = 0; i < maxI; i++)
{
var placeholder = "{";
placeholder += i.toString();
placeholder += "}";
if (title.indexOf(placeholder) != -1)
{
title = title.replace(placeholder,"{0}");
break;
}
}


oGantt.title = formatString(title, subject);



if (overlappedAppointment == false)
{
oGantt.innerText = subject;
}
}





















function LoadSubjectsAsynchronouslyCallback(oResult)
{
if (!oResult.Success)
{
return;
}














if (typeof(oResult.ReturnValue.Subjects.string) == "string")
{
SetTitleAndInnerTextIfNeeded(oResult.ReturnValue.ControlIds.string,
oResult.ReturnValue.Subjects.string,
false );
}
else
{
for (var i = 0; i < oResult.ReturnValue.Subjects.string.length ; i++)
{


var count = 0;
for ( var j = 0 ; j < oResult.ReturnValue.ControlIds.string.length; j++ )
{
if ( oResult.ReturnValue.ControlIds.string[i] ==
oResult.ReturnValue.ControlIds.string[j] )
{
count++;
}
}
var overlappedAppointment = false;
if ( count > 1 )
{
overlappedAppointment = true;
}

SetTitleAndInnerTextIfNeeded(oResult.ReturnValue.ControlIds.string[i],
oResult.ReturnValue.Subjects.string[i],
overlappedAppointment );
}
}


}
function ConfigureGrid()
{
if (!IsNull(crmGrid.InnerGrid))
{
crmGrid.AutoHideHScroll = false;


crmGrid.attachEvent("OnChangePage", OnGridChangePage);
crmGrid.attachEvent("OnSelectionChange", OnGridSelectionChange);
crmGrid.attachEvent("OnBeforeRefresh", OnGridBeforeRefresh);
window.document.all.divDataArea.attachEvent("onscroll", handleGridScroll);
if (crmGrid.InnerGrid.NumberOfRecords > 0)
{
crmGrid.InnerGrid.SelectRecords(0, 0);
}
else
{
window.parent.GanttSelectionChanged();
}
}

var iGridWidth = GetCachedSetting("AB_GridWidth", 207);
document.all.crmGridTd.style.width = iGridWidth;

if (document.all.extraControls.style.display == "none")
{
document.all.bottomSpacerBar.runtimeStyle.height = 22;
}
}


function EndDateChanged()
{
var sEndDate = DateRangeEnd.DataValue;
window.parent.ChangeEndDate(sEndDate);
}

function StartDateChanged()
{
var sStartDate = DateRangeStart.DataValue;
window.parent.ChangeStartDate(sStartDate);
}

function BlockClick()
{
var eSrc = window.event.srcElement;
SelectGridAndGanttRow(GetRowElement(eSrc));

var oBlock = GetBlockElement(eSrc);
if (oBlock != null)
{
if (IsOverlapped(oBlock))
{

OverlappedAppointmentClicked(oBlock);
}
else
{
SelectBlock(oBlock);
}
}
event.cancelBubble = true;
window.parent.GanttSelectionChanged();
}

function BlockDblClick()
{
BlockClick();
if (selectedBlock != null)
{
var queryString = "?id=" + CrmEncodeDecode.CrmUrlEncode(selectedBlock.oid);
openObjEx(selectedBlock.otype, null, null, queryString);
}
}

function RowClick()
{
var oRow = GetRowElement(window.event.srcElement);
if (oRow == null)
{
return;
}

if (selectedRow != oRow)
{
SelectGridAndGanttRow(oRow);
if (oRow.single != undefined)
{
SelectBlock(GetFirstBlock(oRow));
}
else
{
UnselectBlock();
}
window.parent.GanttSelectionChanged();
}
}
function IsOverlapped(oBlock)
{
return (oBlock.className == "ganttBlockOverlapped");
}
function OverlappedAppointmentClicked(oBlock)
{
var numberToInsert = oBlock.otype.split(",").length;
var oRow = GetRowElement(oBlock);
var oLastRow = GetRowElement(oLastOverlappedBlock);
var bExpand = oBlock.children[0].exp != "1"


if (oLastOverlappedBlock != null && oLastRow != null)
{
if (oLastOverlappedBlock.children.length > 0)
{
oLastOverlappedBlock.children[0].src="/_imgs/ApptBook/plus.gif";
oLastOverlappedBlock.children[0].alt=LOCID_TREE_PLUS;
oLastOverlappedBlock.children[0].exp = "";
RemoveRowsFromGantt(oLastRow);
oLastOverlappedBlock = null;
}
}

if (bExpand)
{

oBlock.children[0].src = "/_imgs/ApptBook/minus.gif";
oBlock.children[0].alt = LOCID_TREE_MINUS;
oBlock.children[0].exp = "1";
var objectTypes = oBlock.otype.split(",");
objectTypes.type = "int";
var objectIds = oBlock.oid.split(",");
objectIds.type = "guid";
var command = new RemoteCommand("GanttControlWebService", "CreateOverlappedAppointments");
command.SetParameter("startDate", window.parent.StartDate());
command.SetParameter("endDate", window.parent.EndDate());
command.SetParameter("zoomLevel", window.parent.ZoomLevel());
command.SetParameter("entityTypes", objectTypes);
command.SetParameter("entityId", oRow.oid);
command.SetParameter("blockEntityIds", objectIds);
var result = command.Execute();
if (result.Success)
{
var insertedDiv = InsertRowsToGantt(oRow, numberToInsert, result.ReturnValue);

oLastOverlappedBlock = oBlock;



SetShowHideConflicts(ShouldShowConflict(), insertedDiv);
}
}
SelectRow(oRow);
}
function RemoveRowsFromGantt(oOverlappedRow)
{
var index = GetGridRow(oOverlappedRow);
var oExpandedDiv = document.getElementById("divExpand");
var height = oExpandedDiv.style.pixelHeight;
oOverlappedRow.removeChild(oExpandedDiv);
var oContent = document.all.ganttContent;
var oContentChildren = oContent.children;
var iRowsNumber = oContentChildren.length;
var bSetRowsHeight = true;


for(var i = index+1; i < iRowsNumber; i++)
{
var rowInner = oContentChildren[i];
if (rowInner.className == "ganttGuide")
{
if (bSetRowsHeight)
{

rowInner.rowsheight = parseInt(rowInner.rowsheight,10) - parseInt(height,10);
bSetRowsHeight = false;
}
}
else
{
rowInner.style.posTop = parseInt(rowInner.style.pixelTop,10) - parseInt(height,10);
}
}
ResizeGuides(oContent);
ResizeRowInTable(index, oOverlappedRow.style.pixelHeight);
}
function InsertRowsToGantt(oOverlappedRow, numberToInsert, html)
{
var index = GetGridRow(oOverlappedRow);
var height = oOverlappedRow.style.pixelHeight * numberToInsert;

var oContent = document.all.ganttContent;
var oContentChildren = oContent.children;
var iRowsNumber = oContentChildren.length;
var bSetRowsHeight = true;

for(var i = index + 1; i < iRowsNumber; i++)
{
var rowInner = oContentChildren[i];
if (rowInner.className == "ganttGuide")
{
if (bSetRowsHeight)
{

rowInner.rowsheight = parseInt(rowInner.rowsheight,10) + parseInt(height,10);
bSetRowsHeight = false;
}
}
else
{
rowInner.style.posTop = parseInt(rowInner.style.pixelTop,10) + parseInt(height,10);
}
}
var insertedDiv = CreateDiv(oOverlappedRow, oOverlappedRow.style.pixelHeight-1, height, html);
ResizeGuides(oContent);
ResizeRowInTable(index, height + oOverlappedRow.style.pixelHeight);
return insertedDiv;
}

function CreateDiv(oOverlappedRow, top, height, html)
{
var div=document.createElement("DIV");
div.id="divExpand";
div.style.position = "ABSOLUTE";
div.style.top = top + "px";
if ( LOCID_UI_DIR=="RTL" )
div.style.right = "0px";
else
div.style.left = "0px";
div.style.height = height + "px";
div.innerHTML=html;
oOverlappedRow.appendChild(div);
return div;
}



function GetBlockElement(oElement)
{
if (oElement.tagName == "IMG")
{
oElement = oElement.parentElement;
}
if (oElement.className.indexOf("ganttBlock") != -1)
{
return oElement;
}
return null;
}
function GetRowElement(oElement)
{
if (oElement != null)
{
if (oElement.tagName == "IMG")
{
oElement = oElement.parentElement;
}
if (oElement.className.indexOf("ganttBlock") != -1)
{
oElement = oElement.parentElement;
}
if (oElement.className.indexOf("ganttRow") != -1)
{
return oElement;
}
}
return null
}
function GetFirstBlock(oRow)
{
var aoBlocks = oRow.childNodes;
for (var i=0; i<aoBlocks.length; i++)
{
var oBlock = aoBlocks.item(i);
var oBlockClasses = oBlock.className;


if (oBlockClasses.indexOf("ganttBlockBase") != -1)
{
return oBlock;
}
}
return null;
}



function GetGridRow(oElement)
{
var oGanttRow = GetRowElement(oElement);
if (oGanttRow != null && oGanttRow.parentElement.id == "divExpand")
{
oGanttRow = oGanttRow.parentElement.parentElement;
}
if (oGanttRow != null && (oGanttRow.className.indexOf("ganttRow") != -1))
{
var asRowId = oGanttRow.id.split("_");
return Number(asRowId[1]);
}
return -1;
}

function GetRowElementFromGridRow(iRow)
{
return document.getElementById("row_"+iRow);
}

function UnselectRow()
{
UnselectBlock();
if (selectedRow != null)
{
selectedRow.className = "ganttRowSelectOff";
}
selectedRow = null;
}

function SelectRow(oRow)
{
UnselectRow();
selectedRow = oRow;
if (oRow != null)
{
selectedRow.className = "ganttRowSelectOn";
}
}
function SelectRowAndSingleBlock(oRow)
{
if (oRow != null)
{
SelectRow(oRow)

if (oRow.single != undefined)
{
SelectBlock(GetFirstBlock(oRow));
}
ScrollToSelection();
}
}

function SelectGridAndGanttRow(oRow)
{
UnSelectRowInTable();
SelectRow(oRow);
SelectRowInTable(GetGridRow(oRow));
}
function SelectRowFromGridRow(rowId)
{
var oRow = GetRowElementFromGridRow(rowId);
SelectRowAndSingleBlock(oRow);
}
function SelectBlock(oBlock)
{
UnselectBlock();

selectedBlock = oBlock;
selectedBlock.stockCssClass = selectedBlock.className;
swapGradientColors(selectedBlock);
selectedBlock.className = selectedBlock.stockCssClass + " ganttBlockSelected";

SetBorder(oBlock);
}

function UnselectBlock()
{
if (selectedBlock != null)
{

selectedBlock.className = selectedBlock.stockCssClass;
swapGradientColors(selectedBlock);
var wasSelected = selectedBlock;
selectedBlock = null;
SetBorder(wasSelected);
}
}

function swapGradientColors(o)
{
if (!IsNull(o.filters[0]))
{
var start = o.filters[0].startColorStr;
var end = o.filters[0].endColorStr;
o.filters[0].startColorStr = end;
o.filters[0].endColorStr = start;
}
}
function ScrollToSelection()
{
switch (window.parent.SubAreaType())
{
case "apptbook":
ScrollToBlock(GetFirstBlock(selectedRow), true);
break;
}
}



var _iDest = 0;
function ScrollToPos(iTimeRemaining, iTimeSlice)
{
iTimeRemaining -= iTimeSlice;
if (iTimeRemaining >0)
{
if (JumpToPos(Math.floor((ganttHeaderRowTop.scrollLeft + _iDest) /2)))
{
setTimeout("ScrollToPos("+iTimeRemaining+","+iTimeSlice+")", iTimeSlice);
}
}
else
{
JumpToPos(_iDest);
}
}

function JumpToPos(iPos)
{
var oGantt = window.document.all.ganttContent;
if (IsNull(oGantt))
{
return true;
}
else
{
if (ganttHeaderRowBottom.scrollLeft == iPos && oGantt.scrollLeft == iPos)
{
return false;
}
}




oGantt.scrollLeft = iPos;
if ( LOCID_UI_DIR == "RTL" )
{
if ( oHeaderRowPadding == null )
oHeaderRowPadding = window.document.all.ganttHeaderRowTop.lastChild ;
ganttHeaderRowTop.scrollLeft = oGantt.scrollLeft -iScrollbarWidth + oHeaderRowPadding.clientWidth;
ganttHeaderRowBottom.scrollLeft = oGantt.scrollLeft -iScrollbarWidth + oHeaderRowPadding.clientWidth;
}
else
{
ganttHeaderRowTop.scrollLeft = oGantt.scrollLeft;
ganttHeaderRowBottom.scrollLeft = oGantt.scrollLeft;
}
return true;
}
function ScrollToBlock(oBlock, bSoftScroll)
{
if (oBlock)
{
ScrollToOffset(oBlock.style.posLeft, bSoftScroll);
}
}
function ScrollToOffset(iDest, bSoftScroll)
{
_iDest = iDest;

if (bSoftScroll && _iSmoothScrollLimit > _numberBlocksRendered)
{
ScrollToPos(1000, 100);
}
else
{
JumpToPos(_iDest);
}
}


function ResizeRowInTable(index, height)
{
var rowsList = document.all.gridBodyTable.children[1];
var oRow = rowsList.children[index];
oRow.style.pixelHeight = height;
}
function SelectRowInTable(index)
{
bChangingGrid = true;
document.all.crmGrid.InnerGrid.SelectRecords(index, index, true);
bChangingGrid = false;
}
function UnSelectRowInTable(index)
{
bChangingGrid = true;
document.all.crmGrid.InnerGrid.UnselectRecords();
bChangingGrid = false;
}
function applyStyle(o, sClass)
{
var i = 0;
var ii = o.cells.length;
while (i < ii)
{
o.cells[i].className = sClass;
i++;
}
}
function ResizeContent()
{
var eSrc = window.event.srcElement;
ResizeGuides(eSrc);
ScrollHeader();
}

function ResizeGuides(oContent)
{

if (bChangingGuides || IsNull(oContent))
{
return;
}
bChangingGuides = true;

var iRowHeight = 0;



var iScrollSize = (oContent.offsetWidth-oContent.clientWidth)
var iClientHeight = oContent.offsetHeight - iScrollSize;

var oContentChildren = oContent.children;
var iRowsNumber = oContentChildren.length;


for(var i = 0; i < iRowsNumber; i++)
{
var child = oContentChildren[i];
if (child.className == "ganttGuide")
{
iRowHeight = child.rowsheight;
break;
}
}


var iNewHeight = Math.max(iClientHeight - 1, iRowHeight);

for(var i = 0; i < iRowsNumber; i++)
{
var child = oContentChildren[i];
if (child.className == "ganttGuide")
{
if (iNewHeight == child.posHeight)
{

break;
}
child.style.posHeight = iNewHeight;
}
}
bChangingGuides = false;
}

function ScrollHeader()
{
if (!bScrollSync)
{
bScrollSync = true;
var eSrc = window.event.srcElement;
if ( LOCID_UI_DIR == "RTL" )
{
if ( oHeaderRowPadding == null )
oHeaderRowPadding = window.document.all.ganttHeaderRowTop.lastChild ;
window.document.all.ganttHeaderRowTop.scrollLeft = eSrc.scrollLeft - iScrollbarWidth + oHeaderRowPadding.clientWidth;
window.document.all.ganttHeaderRowBottom.scrollLeft = eSrc.scrollLeft- iScrollbarWidth + oHeaderRowPadding.clientWidth;
}
else
{
window.document.all.ganttHeaderRowTop.scrollLeft = eSrc.scrollLeft;
window.document.all.ganttHeaderRowBottom.scrollLeft = eSrc.scrollLeft;
}
window.document.all.divDataArea.scrollTop = eSrc.scrollTop;
bScrollSync = false;
}
}
function handleGridScroll()
{
var oGantt = window.document.all.ganttContent;
if (!bScrollSync &&  !IsNull(oGantt))
{
bScrollSync = true;
oGantt.scrollTop = window.document.all.divDataArea.scrollTop;
bScrollSync = false;
}
}
function ClearSelection()
{
selectedRow = null;
selectedBlock = null;
}
function SelectedBlockID()
{
if (selectedBlock != null)
{
return selectedBlock.id;
}
return "";
}
function SelectedRowID()
{
if (selectedRow != null)
{
return selectedRow.id;
}
return "";
}

function OnGridBeforeRefresh()
{
var sHref = window.location.search;
sHref = SetQueryString(sHref, "sortColumns", sortColumns.value);
sHref = SetQueryString(sHref, "pagingCookie", "");
crmGrid.DisablePaging();
window.location.search = sHref;
window.event.returnValue = false;
}

function OnGridChangePage()
{
var url = SetQueryString(window.location.search, "pageNum", event.NewPageNumber);
url = SetQueryString(url, "pagingCookie", IsNull(crmGrid.pagingCookie) ? "" : crmGrid.pagingCookie);
url = SetQueryString(url, "filter", IsNull(event.srcElement.GetParameter("filter"))  ? "" : event.srcElement.GetParameter("filter"));
crmGrid.DisablePaging();
window.location.search = url;
event.returnValue = false;
}

function OnGridSelectionChange()
{
if (bChangingGrid)
{
return;
}
var gridRow = document.all.crmGrid.InnerGrid.SelectedRecords[0];
if (!IsNull(gridRow))
{
SelectRowFromGridRow(gridRow[2]);
}
window.parent.GanttSelectionChanged();
}





function Chunk(chunkGuids, chunkTypes, chunkReferences)
{
this.ids = chunkGuids;
this.types = chunkTypes;
this.references = chunkReferences;
this.ids.type = "guid";
this.types.type = "int";
this.length = (IsNull(this.ids)) ? 0 : this.ids.length;

return this;
}
var _iNextBlock = 0;
var _iTotalBlocks = 0;
var _iLen =0;
var _a = null;




function GetNextChunk(iState, rootElement)
{
var chunkGuids = new Array();
var chunkTypes = new Array();
var chunkReferences = new Array();
var j=0;
var a;
if (rootElement != null)
{
a = rootElement.all;
}
if (a == null)
{
a = gridBodyTable.document.all;
}
_iLen = a.length;
var bUpdateBlock =(!IsNull(iState));

for (; _iNextBlock < _iLen && j <= _iChunkSize; _iNextBlock++)
{
o = a[_iNextBlock];
if (o.tagName == "DIV")
{
if (o.otype == Appointment || o.otype == ServiceAppointment || o.className.indexOf("ganttBlockOverlapped") != -1)
{
if (o.className.indexOf("ganttBlockBase") != -1 || o.className.indexOf("ganttBlockOverlapped") != -1)
{
if (j >= _iChunkSize)
{
break;
}




var aBlockIds = o.oid.split(",");
var aBlockTypes = o.otype.split(",");


for (var jj = 0; jj < aBlockIds.length; jj ++)
{
chunkGuids[j] = aBlockIds[jj];
chunkTypes[j] = aBlockTypes[jj];
chunkReferences[j++] = o;
}
if (bUpdateBlock)
{
SetConflictState(o, iState);
}
}
}
}
}
return new Chunk(chunkGuids, chunkTypes, chunkReferences);
}
var CONFLICTSTATE_NORMAL = 0;
var CONFLICTSTATE_CONFLICT = 1;
var CONFLICTSTATE_UNKNOWN = 2;


function SetConflictState(oDiv, iState)
{
oDiv.iState=iState;
SetBorder(oDiv);
}

function SetBorder(oBlock)
{
var iState = oBlock.iState;
if (iState == null)
{
iState = CONFLICTSTATE_NORMAL;
}


if (oBlock.stockColor == null)
{
oBlock.stockColor = oBlock.currentStyle.borderColor;
}


if (iState == CONFLICTSTATE_CONFLICT)
{
oBlock.runtimeStyle.borderColor = "red";
}
else if (oBlock == selectedBlock)
{
oBlock.runtimeStyle.borderColor = "black";
}
else
{
oBlock.runtimeStyle.borderColor = oBlock.stockColor;
}


if (oBlock == selectedBlock || iState == CONFLICTSTATE_CONFLICT)
{
oBlock.runtimeStyle.borderWidth = "2px";
}
else
{
oBlock.runtimeStyle.borderWidth = "1px";
}


if (iState == CONFLICTSTATE_UNKNOWN)
{
oBlock.runtimeStyle.borderStyle = "dotted";
}
else
{
oBlock.runtimeStyle.borderStyle = "solid";
}
}
var _waitTime = 500;
var _asyncValidate = null;

function UpdateResults(result)
{
if (!_commandValidate)
{
return;
}



if (result.Success && result.ReturnValue != "" && result.ReturnValue.OperationSuccess)
{
var conflicts = result.ReturnValue.Results.boolean;
if (!IsNull(conflicts))
{
var iLast = _chunk.references.length;
if (iLast==1)
{
UpdateGanttBlock(_chunk.references[0], conflicts);
}
else
{
for (var i=0; i<iLast; i++)
{
if (!IsNull(conflicts[i]))
{
if (!_commandValidate)
{
break;
}
UpdateGanttBlock(_chunk.references[i], conflicts[i]);
}
}
}
}
}
if (_commandValidate)
{

if (_iNextBlock < _iLen)
{
if (_commandValidate)
{
_asyncValidate = setTimeout("ValidateTimeBlocks()", _waitTime);
}
}
else
{
_commandValidate = null;
ShowHideProgresBar(false);
validationProgress.SetSteps(0);
}
}
}
function UpdateGanttBlock(oBlock, bHasConflict)
{



if (IsNull(oBlock.iState) || oBlock.iState == CONFLICTSTATE_UNKNOWN || bHasConflict)
{
SetConflictState(oBlock, (bHasConflict ? CONFLICTSTATE_CONFLICT : CONFLICTSTATE_NORMAL));
}


_iTotalProcessed++;
validationProgress.SetPercentage(_iTotalProcessed *100 /_iTotalBlocks);
}
var _chunk = null;
var _commandValidate = null;
function ValidateTimeBlocks()
{
_chunk = GetNextChunk();
if (_chunk.length > 0)
{
_commandValidate = new RemoteCommand("ActivitiesWebService", "ValidateMultiple");

_commandValidate.SetParameter("ids", _chunk.ids);
_commandValidate.SetParameter("objectTypeCodes", _chunk.types);
_commandValidate.Execute(UpdateResults);
}
}
function ToggleShowConflicts()
{
SetShowHideConflicts(!ShouldShowConflict());
}

function ShouldShowConflict()
{
return GetCachedSetting("AB_ShowConflict", false);
}
function SetShowConflict(bTrue)
{
SetCachedSetting("AB_ShowConflict", bTrue);
}
function SetConflictButtonState(bShowConflict)
{
if (!IsNull(window.parent) && !IsNull(window.parent.SetConflictButtonState))
{
window.parent.SetConflictButtonState(bShowConflict);
}
}

function SetShowHideConflicts(bShowConflict, rootElement)
{
if (ShouldShowConflict() && _commandValidate)
{


rootElement = gridBodyTable.document;
if (_commandValidate)
{
_commandValidate.Abort();
_commandValidate = null;
}
if (_asyncValidate != null)
{
clearTimeout(_asyncValidate);
_asyncValidate = null;
}
ShowHideProgresBar(false);
}

SetShowConflict(bShowConflict);
SetConflictButtonState(ShouldShowConflict());

_iNextBlock = 0;
_iTotalBlocks = 0;
_iTotalProcessed = 0;
_iLen = gridBodyTable.document.all.length;
if (ShouldShowConflict())
{
var chunk = null;
while (_iNextBlock < _iLen)
{
chunk = GetNextChunk(CONFLICTSTATE_UNKNOWN, rootElement);
_iTotalBlocks += chunk.ids.length;
}

if (_iTotalBlocks > 0)
{
ShowHideProgresBar(ShouldShowConflict());

_iNextBlock = 0;
ValidateTimeBlocks();
}
}
else
{
SetAllBlockState(CONFLICTSTATE_NORMAL, _iLen);
}
}
function ShowHideProgresBar(bShow)
{
if (bShow)
{
progressDiv.style.display = "inline";
zoom.style.display = "none";
}
else
{
progressDiv.style.display = "none";
zoom.style.display = "inline";
}
}

function SetAllBlockState(iState, iLen)
{
if (IsNull(iLen))
{
iLen = gridBodyTable.document.all.length;
}
_iNextBlock = 0;

while (_iNextBlock < iLen)
{
GetNextChunk(iState, gridBodyTable.document);
}
_iNextBlock = 0;
}
function UpdateConflictButtonState()
{
if (ShouldShowConflict())
{
SetShowHideConflicts(true);
}
}





function auto(iObjType)
{
window.parent.auto();
}

function ZoomChanged()
{
var zoomLevel = zoom.SelectedLevel;
if (!IsNull(window.parent) && !IsNull(window.parent.SetZoomLevel))
{
window.parent.SetZoomLevel(zoomLevel);
}
}


var bIsPressed = false;
var iLastGridSize = 0;
var iLeftLimit = 0;
var iRightLimit = 0;
var iDownX = 0;
if ( LOCID_UI_DIR=="RTL")
{
var iLeftMargin = 30;
var iRightMargin = 10;
}
else
{
var iLeftMargin = 10;
var iRightMargin = 30;
}
var iScrollbarWidth = 17 ;
function MouseDown()
{
bIsPressed = true;
iLastGridSize = 0;
CrmGanttForm.cursor = "col-resize";
oSeparator.setCapture();

iDownX = window.event.x;
var iSeparatorMouseX = (iDownX - oSeparator.offsetLeft);
iLeftLimit = iLeftMargin + iSeparatorMouseX;
iRightLimit = window.document.body.clientWidth - (iRightMargin - iSeparatorMouseX);
}
function MouseMove()
{
if (!bIsPressed)
{
return;
}
var mousePos = window.event.x;
if ((mousePos >= iLeftLimit) && (mousePos <= iRightLimit))
{
resizeSpliter(mousePos);
}
else if (mousePos < iLeftLimit)
{
resizeSpliter(iLeftLimit);
}
else
{
resizeSpliter(iRightLimit);
}
}
function MouseUp()
{
oSeparator.releaseCapture();
bIsPressed = false;
CrmGanttForm.cursor = "default";
if (iLastGridSize > 0)
{
SetCachedSetting("AB_GridWidth", iLastGridSize);
}
}
function resizeSpliter(mousePos)
{
iLastGridSize = mousePos - 4;
if ( LOCID_UI_DIR=="RTL")
iLastGridSize = window.document.body.clientWidth - iLastGridSize ;
document.all.crmGridTd.style.width = iLastGridSize;
}

function adjustBiDiStartPos()
{
var startTime = _TotalHoursDisplayed-_TotalMinutes/60 ;
startTime = (startTime*60*_TimeBlockWidth)/_SectionMinutes-window.document.all.ganttContent.clientWidth ;

return (startTime)+_Margin;
}
